<template>
  <div class="home pa-3">
    <van-image
      :src="require('@/assets/images/home/banner.png')"
      @click="goTo({ name: 'home-popup-image' })"
    ></van-image>
    <section class="home__entries mt-5 mb-10 d-flex flex-warp flex-align-between">
      <div
        v-for="item in entries"
        :key="item.label"
        class="home__entries-item d-flex flex-column flex-center mb-3 rounded-md-l"
        @click.stop="onItemClick(item)"
      >
        <van-image :src="require(`@/assets/images/home/${item.imageCode}.png`)" width="1.333rem"></van-image>
        <span class="text--primary font-weight-medium mt-4">{{ item.label }}</span>
      </div>
    </section>
  </div>
</template>

<router>
{
  path:'/'
}
</router>

<script>
import { isAbsoluteUrl, absoluteGo } from '@health/utils';
import { entries } from './utils/config';

export default {
  name: 'Home',
  layout: 'tabbar',
  asyncData({ app, route, query, redirect, $session }) {
    let isAbsoluteRedirectPath = true;
    let redirectPath = $session.get('homeUrl'); // 第三方通过 query._homeUrl 携带的优先
    // 配置自定义首页
    if (redirectPath) {
      if (typeof redirectPath === 'string' && isAbsoluteUrl(redirectPath)) {
        isAbsoluteRedirectPath = true;
      } else {
        const { resolved } = app.router.resolve(
          typeof redirectPath === 'string' ? { path: redirectPath } : redirectPath,
        );
        // 应用内页面直接导航，否则以绝对地址方式打开
        if (resolved.matched.length) {
          isAbsoluteRedirectPath = false;
        } else {
          isAbsoluteRedirectPath = true;
        }
        redirectPath = resolved.path;
      }
      // 如果和当前的路由不一样
      if (redirectPath !== route.path) {
        if (isAbsoluteRedirectPath) {
          absoluteGo(redirectPath);
          return new Promise((resolve) => {});
        } else {
          return redirect({ path: redirectPath, query });
        }
      }
    }
  },
  data() {
    return {
      entries: entries(),
    };
  },
  methods: {
    async onItemClick(item) {
      if (item.code) {
        const { data } = await this.$$lvtongApis.getEquityCard(item.code)({ loading: true });
        const cardId = data[0]?.id || '';
        const cardItemId = data[0]?.cardItemList[0]?.id || '';
        if (!cardId || !cardItemId) {
          return this.$dialog.alert({ message: '您暂时没有权益！', confirmButtonText: '关闭' });
        }
      }

      this.goTo(item.to);
    },
  },
};
</script>
<style lang="less" scoped>
.home {
  &__entries {
    &-item {
      width: 169px;
      height: 169px;
      background-color: @gray-1;
    }
  }
}
</style>
